﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleMenu
{
    class VedioFileService
    {
        private VedioFileService()
        {
            InitData();
        }
        private static VedioFileService T;
        public static VedioFileService Instance()
        {
            if (T == null)
                T = new VedioFileService();
            return T;
        }

        private List<Vedio> VedioFiles = new List<Vedio>();

        //新增一个文件
        public bool AddFile(Vedio file)
        {
            foreach (Vedio f in VedioFiles)
            {
                if (f.Name == file.Name)
                    return false;
            }
            this.VedioFiles.Add(file);
            return true;
        }
        //根据文件名删除文件
        public bool RemoveFile(string fileName)
        {
            for (int i = 0; i < this.VedioFiles.Count; i++)
            {
                if (this.VedioFiles[i].Name == fileName)
                {
                    this.VedioFiles.RemoveAt(i);
                    return true;
                }
            }
            return false;
        }
        public bool RemoveAllFile()
        {
            for (int i = 0; i < this.VedioFiles.Count; i++)
            {
                this.VedioFiles.RemoveAt(i);
            }
            return true;
        }
        //根据文件名查找一个文件
        public Vedio GetFile(string fileName)
        {
            for (int i = 0; i < this.VedioFiles.Count; i++)
            {
                if (this.VedioFiles[i].Name == fileName)
                {
                    return this.VedioFiles[i];
                }
            }
            return null;
        }
        //获取所有文件
        public List<Vedio> GetAllFiles()
        {
            return this.VedioFiles;
        }

        //从本地读取文件
        public bool InitData()
        {
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(new FileStream("data.txt", FileMode.OpenOrCreate));
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] datas = line.Split(',');
                    Vedio file = new Vedio(datas[0], datas[1], int.Parse(datas[2]));
                    this.AddFile(file);
                }
            }
            catch { }
            finally
            {
                sr.Close();
            }
            return false;
        }
        //保存文件到本地
        public bool Save()
        {
            StreamWriter sw = null;
            try
            {
                File.Delete("data.txt");
                sw = new StreamWriter(new FileStream("data.txt", FileMode.Create));
                foreach (Vedio file in this.VedioFiles)
                {
                    sw.WriteLine(file.Path + "," + file.Name + "," + file.Size);
                }
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                sw.Close();
            }
            return false;
        }
    }
}
